package com.example.glide;

import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;

import java.util.Random;

/**
 * @author by liliang on 2020/6/16 2:09 AM
 */
public class Glide4Activity extends AppCompatActivity {
    private static final String TAG = "Glide4Activity";

    static String[] strings = {"https://www.logosc.cn/uploads/resources/2018/11/26/1543212390.jpg",
            "https://n.sinaimg.cn/tech/transform/769/w299h470/20200615/fda5-iuzasxs3366211.gif",
    "https://n.sinaimg.cn/tech/transform/584/w294h290/20200615/33dc-iuzasxs3353884.gif",
    "https://www.logosc.cn/uploads/resources/2018/11/05/1541412649.jpg",
            "http://gank.io/images/f4f6d68bf30147e1bdd4ddbc6ad7c2a2",
            "http://pic.netbian.com/uploads/allimg/200121/230803-157961928349a7.jpg",
            "http://pic.netbian.com/uploads/allimg/191105/234706-157296882619b0.jpg",
            "http://pic.netbian.com/uploads/allimg/190902/152344-1567409024d50f.jpg"};
    String imgUrl = "http://gank.io/images/f4f6d68bf30147e1bdd4ddbc6ad7c2a2";
    ImageView imageView;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.avatar);
        initView(imgUrl);
    }

    public void initView(final String img) {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMessage("加载中");
        ProgressInterceptor.addListener(img, new ProgressListener() {
            @Override
            public void onProgress(int progress) {
                Log.d(TAG, "onProgress: " + progress);
                progressDialog.setProgress(progress);
            }
        });

        Glide.with(this)
                .load(img)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .into(new CustomTarget<Drawable>(300, 300) {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        progressDialog.dismiss();
                        if (resource instanceof GifDrawable) {
                            ((GifDrawable) resource).start();
                        }
                        imageView.setImageDrawable(resource);
                        Log.d(TAG, "CustomTarget onResourceReady: ");
                        ProgressInterceptor.removeListener(img);
                    }

//                    @Override
//                    public void onStart() {
//                        super.onStart();
//                        Log.d(TAG, "CustomTarget onStart: ");
//                        if (imageView.getDrawable() != null) return;
//                        Log.d(TAG, progressDialog.getProgress() + "");
//                        progressDialog.show();
//                    }

                    @Override
                    public void onLoadStarted(@Nullable Drawable placeholder) {
                        super.onLoadStarted(placeholder);
                        Log.d(TAG, "CustomTarget onLoadStarted: ");
                        progressDialog.show();
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                    }
                });

    }

    public void loadImage(View view) {
        Random rand=new Random();
       final int index =rand.nextInt(strings.length);
        System.out.println(index);
        initView(strings[index]);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");

    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart");
    }
}